<template>
  <van-cell class="comment-item">
    <template #icon>
    <van-image
      class="avatar"
      round
      fit="cover"
      :src="comment.aut_photo"
    />
    </template>
    <template #title>
    <div class="title-wrap">
      <div class="user-name">{{comment.aut_name}}</div>
      <!-- 点赞 -->
      <van-button
        class="like-btn"
        :class='{liked:comment.is_liking}'
        :icon="comment.is_liking? 'good-job':'good-job-o'"
        :loading="loading"
        @click="onCommentLike"
      >{{comment.like_count || '赞'}}</van-button>
    </div>
    </template>

<template #label>
    <div>
      <p class="comment-content">{{comment.content}}</p>
      <div class="bottom-info">
        <span class="comment-pubdate">{{dayjs().to(dayjs(comment.pubdate))}}</span>
        <van-button
          class="reply-btn"
          @click="$emit('reply-click',comment)"
          round
        >回复 {{comment.reply_count}}</van-button>
      </div>
    </div>
    </template>
  </van-cell>
</template>

<script>
import dayjs from 'dayjs'
import { ref } from 'vue'
import { Toast } from 'vant'
import { addCommentLike, deleteCommentLike } from '@/api/comment'
export default {
  name: 'CommentItem',
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  setup (props, context) {
    const loading = ref(false)
    async function onCommentLike () {
      loading.value = true
      try {
        if (props.comment.is_liking) {
        // 取消点赞
          await deleteCommentLike(props.comment.com_id)
          context.emit('deleteCommentLike')
        } else {
          // 点赞
          await addCommentLike(props.comment.com_id)
          context.emit('addCommentLike')
        }
      } catch (err) {
        if (err.response && err.response.status === 401) {
          Toast('请先登录')
        } else {
          Toast('操作失败')
        }
      }
      loading.value = false
    }
    return {
      dayjs,
      loading,
      onCommentLike
    }
  }
}
</script>

<style scoped lang="less">
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    // width: 135px;
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
    &.liked {
      color:#e5645f
    }
  }
}
</style>
